package com.example.myapplication7;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;

public class AnalysisFragment extends Fragment {
    private BarChart barChart;
    private LineChart lineChart;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_analysis, container, false);
        barChart = view.findViewById(R.id.barChart);
        lineChart = view.findViewById(R.id.lineChart);

        // 模拟新闻点赞数据（柱状图）
        List<BarEntry> barEntries = new ArrayList<>();
        barEntries.add(new BarEntry(0, 100)); // 新闻1点赞
        barEntries.add(new BarEntry(1, 150)); // 新闻2点赞
        barEntries.add(new BarEntry(2, 80));  // 新闻3点赞

        // 模拟用户诉求数据（折线图）
        List<Entry> lineEntries = new ArrayList<>();
        lineEntries.add(new Entry(0, 50));  // 诉求1
        lineEntries.add(new Entry(1, 70));  // 诉求2
        lineEntries.add(new Entry(2, 120)); // 诉求3

        // 渲染柱状图
        BarDataSet barDataSet = new BarDataSet(barEntries, "新闻点赞数");
        barDataSet.setColor(getResources().getColor(android.R.color.holo_blue_light));
        barChart.setData(new BarData(barDataSet));
        barChart.getDescription().setText("新闻点赞分布");
        barChart.invalidate();

        // 渲染折线图
        LineDataSet lineDataSet = new LineDataSet(lineEntries, "用户诉求数");
        lineDataSet.setColor(getResources().getColor(android.R.color.holo_red_light));
        lineChart.setData(new LineData(lineDataSet));
        lineChart.getDescription().setText("用户诉求趋势");
        lineChart.invalidate();

        return view;
    }
}